// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "RPG.h"
#include "RPGWorldSettings.h"
#include "DataTableFunctions.h"

const FString UDataTableFunctions::ContextString(TEXT("GENERAL"));

UDataTable* UDataTableFunctions::GetItensDataTable()
{
	TObjectIterator<APickupActor> Object;
	if (!Object)
	{
		UE_LOG(LogClass, Warning, TEXT("Object is NULL."));
		return NULL;
	}

	UWorld* world = Object->GetWorld();
	if (!world)
	{
		UE_LOG(LogClass, Warning, TEXT("World is NULL."));
		return NULL;
	}

	ARPGWorldSettings* settings = Cast<ARPGWorldSettings>(world->GetWorldSettings());
	if (!settings)
	{
		UE_LOG(LogClass, Warning, TEXT("Settings is NULL."));
		return NULL;
	}

	return settings->ItemDataTable;
}

FInventoryItemStruct UDataTableFunctions::GetItemData(const int32& ItemID)
{
	FInventoryItemStruct* item = GetItemDataPtr(ItemID);

	if (!item)
	{
		UE_LOG(LogClass, Warning, TEXT("Item pointer is NULL."));
		return FInventoryItemStruct();
	}

	GetAssetPtr(item->Modelo);
	GetAssetPtr(item->Icone);

	return *item;
}

FInventoryItemStruct* UDataTableFunctions::GetItemDataPtr(const int32& ItemID)
{
	UDataTable* table = GetItensDataTable();
	if (!table)
	{
		UE_LOG(LogClass, Error, TEXT("GetItemDataTable() returned NULL."));
		return NULL;
	}

	FName rowName = FName(*FString::FromInt(ItemID));
	FInventoryItemStruct* row = table->FindRow<FInventoryItemStruct>(rowName, ContextString);

	return row;
}